# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     conf
   Description :
   Author :       Jolg
   date：          2021/11/4
-------------------------------------------------
   Change Activity:
                   2021/11/4:
-------------------------------------------------
"""

import io
import requests

class Dict2Obj(dict):
    def __init__(self, *args, **kwargs):
        super(Dict2Obj, self).__init__(*args, **kwargs)

    def __getattr__(self, key):
        value = self[key]
        if isinstance(value, dict):
            value = Dict2Obj(value)
        return value

def createFile(stream, path, req = None, meta = False):
    bufSize = 8196
    with io.open(path, 'wb') as out:
        read = stream.read(bufSize)
        while len(read) > 0:
            out.write(read)
            read = stream.read(bufSize)
    return

def saveFileFromUri(uri, path, req = None, meta = False):
    resp = requests.get(uri, stream=True)
    createFile(resp.raw, path, req, meta)
    return

def formatFileSize(sizeBytes):
    sizeBytes = float(sizeBytes)
    result = float(abs(sizeBytes))
    suffix = "B";
    if (result > 1024):
        suffix = "KB"
        mult = 1024
        result = result / 1024
    if (result > 1024):
        suffix = "MB"
        mult *= 1024
        result = result / 1024
    if (result > 1024):
        suffix = "GB"
        mult *= 1024
        result = result / 1024
    if (result > 1024):
        suffix = "TB"
        mult *= 1024
        result = result / 1000
    if (result > 1024):
        suffix = "PB"
        mult *= 1024
        result = result / 1024
    return format(result, '.2f') + " " + suffix

